package com.ququ.syb.mqhandler.service;

import com.alibaba.fastjson.JSONObject;
import com.ququ.common.result.ResultJson;
import com.ququ.syb.common.constant.RabbitMqQueueNameConstant;
import com.ququ.syb.common.constant.ResultStatusConstant;
import com.ququ.syb.common.constant.WithdrawStatusConstant;
import com.ququ.syb.common.pojo.PayChannel;
import com.ququ.syb.common.pojo.PayOrder;
import com.ququ.syb.common.pojo.WithdrawOrder;
import com.ququ.syb.mqhandler.fegin.*;
import com.ququ.syb.mqhandler.rabbitmq.RabbitMQSender;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class WithdrawZhongmaoService {

    private Logger logger = LoggerFactory.getLogger(WithdrawZhongmaoService.class);
    @Autowired
    private ChannelsRouterFegin channelsRouterFegin;
    @Autowired
    private WithdrawOrderFegin withdrawOrderFegin;
    @Autowired
    private WithdrawBusinessFegin withdrawBusinessFegin;
    @Autowired
    private PayChannelFegin payChannelFegin;
    @Autowired
    private RabbitMQSender rabbitMQSender;
    @Autowired
    private PayOrderFegin payOrderFegin;

    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");

    public void doWithdraw(String tradenum){
        WithdrawOrder withdrawOrder = withdrawOrderFegin.queryWithdrawOrder(tradenum).getData();
        if (withdrawOrder == null){
            logger.error("{}提现订单不存在",tradenum);
            return;
        }
        if (withdrawOrder.getStatus() != WithdrawStatusConstant.AUDIT_SUCCESS){
            logger.error("{}提现订单不是审核完成状态，无法重复调用通道提现接口",tradenum);
            return;
        }
        PayChannel payChannel = payChannelFegin.getPayChannel(withdrawOrder.getPaychannelid()).getData();
        if (payChannel.getAllowwithdraw() != null && payChannel.getAllowwithdraw() == 0){
            logger.error("{}通道不允许提现操作，暂不处理",withdrawOrder.getPaychannelid());
            return;
        }
        PayOrder payOrder = payOrderFegin.getPayOrder(withdrawOrder.getPaytradenum()).getData();
        if (withdrawOrder.getWithdrawtype() == null) {
            withdrawOrder.setWithdrawtype(simpleDateFormat.format(payOrder.getPaytime()).equals(simpleDateFormat.format(new Date()))?0:1);
        }
        JSONObject extParams = new JSONObject();
        extParams.put("paytradenum",withdrawOrder.getPaytradenum());
        extParams.put("paydate",simpleDateFormat.format(payOrder.getPaytime()));
        extParams.put("d0",withdrawOrder.getWithdrawtype());
        ResultJson withdrawResult = channelsRouterFegin.withdraw(withdrawOrder.getAccountid(),
                withdrawOrder.getPaychannelid(),
                withdrawOrder.getTradenum(),
                withdrawOrder.getAmount()-withdrawOrder.getCustomercommission(),
                null,null,null,null,null,null,null,null,null,null,null,
                extParams.toJSONString());
        switch (withdrawResult.getStatus()) {
           case  ResultStatusConstant.SUCCESS :
                //提现操作成功，等待回调
                withdrawOrder.setExcutetime(new Date());
                withdrawOrder.setStatus(WithdrawStatusConstant.ACCEPTING);
                withdrawOrderFegin.updateWithdrawOrder(withdrawOrder);
                break;
            case 1001:
                //通道D1余额不足，等待结算后重试
                logger.debug("{}提现订单D1余额不足，等10分钟重重试",tradenum);
                rabbitMQSender.send(RabbitMqQueueNameConstant.AUDIT_WITHDRAWAL_SUCCESS_DEAD,tradenum,10*60*1000L);
                break;
            default:
                //提现操作失败
                withdrawBusinessFegin.withdrawCallback(tradenum, 0, withdrawResult.getMsg());
        }
    }
}
